﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WSNs
{
    public partial class Form_DataBack : Form
    {
        public Action<DataBackStructure> DataBackCmdEvent;
        private List<CtlNode> _listNode = new List<CtlNode>();
        public Form_DataBack(List<CtlNode> node)
        {
            _listNode.Clear();
            _listNode = node;
            InitializeComponent();
        }

        private void Form_DataBack_Load(object sender, EventArgs e)
        {
            if (_listNode.Count == 0)
            {
                MessageBox.Show("等待节点组进网络！", "传输错误");
                this.Close();
            }
            cb_Mode.SelectedIndex = 0;
            cb_Location.SelectedIndex = 0;
            cb_Count.SelectedIndex = 0;

            cb_Node.Items.Add("所有节点");
            cb_Node.SelectedIndex = 0;
            foreach (CtlNode node in _listNode)
            {
                cb_Node.Items.Add(node.NodeID);
                tb_FileID.Text = node.AcqStructure.FileID.ToString();
            }
        }

        private void cb_Count_TextChanged(object sender, EventArgs e)
        {
            try
            {
                UInt32 count = UInt32.Parse(cb_Count.Text);
                tb_Start.Enabled = true;
            }
            catch
            {
                tb_Start.Enabled = false;
                tb_Start.Text = "0";
            }
        }

        private void cb_Mode_SelectedIndexChanged(object sender, EventArgs e)
        {
            gb_Count.Enabled = cb_Mode.SelectedIndex == 0;
        }

        private void btn_Ok_Click(object sender, EventArgs e)
        {
            DataBackStructure dataBackStruct = new DataBackStructure();
            switch (cb_Mode.SelectedIndex)
            {
                case 0:
                    dataBackStruct.TransferMode = TransferMode.Start;
                    break;
                case 1:
                    dataBackStruct.TransferMode = TransferMode.FileName;
                    break;
                case 2:
                    dataBackStruct.TransferMode = TransferMode.StatusPtr;
                    break;
            }
            try
            {
                byte id = byte.Parse(cb_Node.Text);
                dataBackStruct.NodeID = id; //指定节点
            }
            catch
            {
                dataBackStruct.NodeID = 0; //所有节点
            }
            dataBackStruct.FileID = byte.Parse(tb_FileID.Text);
            dataBackStruct.StorageLocation = (StorageLocation) (cb_Location.SelectedIndex + 1);
            dataBackStruct.StartIndex = UInt32.Parse(tb_Start.Text);
            try
            {
                dataBackStruct.Count = UInt32.Parse(cb_Count.Text);
            }
            catch
            {
                dataBackStruct.Count = 0xffffffff;
            }

            DataBackCmdEvent(dataBackStruct);
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
